package logic

import (
	"activity_srv/activity"
	"activity_srv/internal/dao"
	"activity_srv/internal/svc"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGroupLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGroupLogic {
	return &CreateGroupLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGroupLogic) CreateGroup(in *activity.CreateGroupRequest) (*activity.CreateGroupResponse, error) {
	// 记录请求参数日志
	l.Logger.Infof("创建拼团请求参数: UserId=%d, ProductId=%d, ProductNum=%d, GroupNum=%d, ProductPrice=%f, UserOrderSn=%s, GroupType=%d",
		in.UserId, in.ProductId, in.ProductNum, in.GroupNum, in.ProductPrice, in.UserOrderSn, in.GroupType)

	// 实现创建拼团
	group, err := dao.CreateGroup(in)
	if err != nil {
		// 记录详细的错误信息
		l.Logger.Errorf("创建拼团失败: %v", err)
		// 返回具体的错误信息，而不是固定消息
		return nil, fmt.Errorf("拼团创建失败: %v", err)
	}

	// 记录成功日志
	l.Logger.Infof("拼团创建成功: GroupId=%d, GroupSn=%s, UserOrderSn=%s, InviteCode=%s",
		group.Id, group.GroupSn, group.UserOrderSn, group.InviteCode)

	return &activity.CreateGroupResponse{
		Message:     "拼团创建成功",
		GroupId:     int64(group.Id),
		GroupSn:     group.GroupSn,
		UserOrderSn: group.UserOrderSn,
		InviteCode:  group.InviteCode,
	}, nil
}
